package com.smiier.skin;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OActionSheet;
import cn.o.app.ui.OActionSheet.OActionItem;
import cn.o.app.ui.OActionSheet.OnActionItemClickListener;
import cn.o.app.ui.OImageView;
import cn.skinapp.R;

import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.pifuke.adapter.UserAdapter;
import com.smiier.skin.extra.UserExtra;
import com.smiier.skin.net.CostPayTask;
import com.smiier.skin.net.CostPayTask.CostPayItem;
import com.smiier.skin.net.CostPayTask.CostPayRequest;
import com.smiier.skin.net.CostPayTask.CostPayResponse;
import com.smiier.skin.net.DoctorUserGetListTask;
import com.smiier.skin.net.DoctorUserGetListTask.DoctorUserGetListRequest;
import com.smiier.skin.net.DoctorUserGetListTask.DoctorUserGetListResponse;
import com.smiier.skin.net.QuestionCheckAnsweringTask;
import com.smiier.skin.net.QuestionCheckAnsweringTask.QuestionCheckAnsweringRequest;
import com.smiier.skin.net.QuestionCheckAnsweringTask.QuestionCheckAnsweringResponse;
import com.smiier.skin.net.entity.User;
import com.smiier.skin.net.entity.UserWrapper;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.umeng.analytics.MobclickAgent;

/*
 * 选择解答问题的医生或者是悬赏解答
 */
public class DoctorSelectorActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener {
	private PullToRefreshListView mListView;
	private UserAdapter mAdapter;
	private long mStartId = 0;
	private View mHead;
	ArrayList<UserWrapper> Users = new ArrayList<UserWrapper>();
	User mUser;
	TextView mDoctorReward;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.doctor_selector_activity);
		init();
		UserExtra extra = new UserExtra();
		extra.getFrom(getIntent());
		mUser = extra.getUser();
		setNavTitle("指定医生");
		setNavRightBtn("清除");
		mDoctorReward = findViewById(R.id.doctor_reward, TextView.class);
		if (mUser != null && mUser.Name.equals("悬赏金额")) {
			mDoctorReward.setText("悬赏金额(" + mUser.Cost + ")");
		}
		mListView = findViewById(R.id.list, PullToRefreshListView.class);
		mAdapter = new UserAdapter();
		mAdapter.setUser(mUser);
		mListView.setAdapter(mAdapter);
		mAdapter.setActivity(this);
		mHead = findViewById(R.id.select_price);
		mHead.setOnClickListener(mListener);
		ListView list = mListView.getListView();

		// list.setDivider(getResources().getDrawable(
		// R.drawable.border_blue_drawable));
		// list.setDividerHeight(1);
		mListView.setOnItemClickListener(this);

		mListView.setScrollLoadEnabled(true);
		mListView.setPullRefreshEnabled(true);
		mListView.setOnRefreshListener(this);
		mListView.doPullRefreshing(true);
		loadData();
	}

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.text_right) {
			Intent i = new Intent();
			UserExtra extra = new UserExtra();
			extra.setUser(null);
			if (extra.putTo(i)) {
				setResult(RESULT_OK, i);
				finish();
			}
		} else if (id == R.id.btn_left) {

		}
	}

	private OnClickListener mListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (GlobalSettings.mCostPay.size() < 1) {
				CostPayTask task = new CostPayTask();
				task.setListener(new NetTaskListener<CostPayRequest, CostPayResponse>() {

					@Override
					public void onComplete(
							INetTask<CostPayRequest, CostPayResponse> task,
							CostPayResponse response) {
						if (response != null) {
							GlobalSettings.mCostPay = response;
							showDialog();
						}
					}

					@Override
					public void onException(
							INetTask<CostPayRequest, CostPayResponse> task,
							Exception e) {

					}
				});
				add(task);
			} else {
				showDialog();
			}

		}
	};

	private void showDialog() {
		ArrayList<OActionItem> dataProvider = new ArrayList<OActionSheet.OActionItem>();
		OActionItem item = null;
		for (CostPayItem cost : GlobalSettings.mCostPay) {
			item = new OActionItem();
			item.setText("悬赏￥" + cost.Cost + "元");
			dataProvider.add(item);
		}
		OActionSheet<OActionItem> actionSheet = new OActionSheet<OActionSheet.OActionItem>(
				DoctorSelectorActivity.this);
		actionSheet.setCancel("取消");
		actionSheet.setDataProvider(dataProvider);
		actionSheet.show(false, true);
		actionSheet
				.setOnActionItemClickListener(new OnActionItemClickListener<OActionSheet.OActionItem>() {

					@Override
					public void onItemClick(OActionSheet<OActionItem> sheet,
							View v, int position, OActionItem dataItem) {
						double price = GlobalSettings.mCostPay.get(position).Cost;
						User user = new User();
						user.Name = "悬赏金额";
						user.Cost = price;
						Intent i = new Intent();
						UserExtra extra = new UserExtra();
						extra.setUser(user);
						if (extra.putTo(i)) {
							setResult(RESULT_OK, i);
							finish();
						}

					}
				});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final OImageView image = (OImageView) view
				.findViewById(R.id.imgLayout_check);
		final UserWrapper wrapper = mAdapter.getDataProvider().get(position);
		if (mUser != null && (mUser.Uid + "").equals(wrapper.User.Uid + "")
				&& image.getVisibility() == View.VISIBLE) {
			image.setVisibility(View.GONE);
		} else {
			image.setVisibility(View.VISIBLE);
			QuestionCheckAnsweringTask task = new QuestionCheckAnsweringTask();
			QuestionCheckAnsweringRequest req = new QuestionCheckAnsweringRequest();

			req.token = GlobalSettings.sToken;
			req.uids = new ArrayList<Long>();
			req.uids.add(wrapper.User.Uid);
			task.setRequest(req);
			task.addListener(new NetTaskListener<QuestionCheckAnsweringTask.QuestionCheckAnsweringRequest, QuestionCheckAnsweringTask.QuestionCheckAnsweringResponse>() {
				@Override
				public void onComplete(
						INetTask<QuestionCheckAnsweringRequest, QuestionCheckAnsweringResponse> task,
						QuestionCheckAnsweringResponse response) {
					if (response != null
							&& response.ResultCode == 200
							&& !response.Res.toString().contains(
									wrapper.User.Uid + "")) {
						Intent i = new Intent();
						UserExtra extra = new UserExtra();
						extra.setUser(wrapper.User);
						if (extra.putTo(i)) {
							setResult(RESULT_OK, i);
							finish();
						}
					} else {
						toast("医生还有您的问题未解答");
					}
					image.setVisibility(View.GONE);
				}

				@Override
				public void onException(
						INetTask<QuestionCheckAnsweringRequest, QuestionCheckAnsweringResponse> task,
						Exception e) {
					e.printStackTrace();
				}
			});
			add(task);
		}
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		mStartId = 0;
		loadData();
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		loadData();
	}

	private void loadData() {
		DoctorUserGetListRequest req = new DoctorUserGetListRequest();
		req.start = mStartId;
		req.pagesize = 10;
		req.sort = 0;
		DoctorUserGetListTask task = new DoctorUserGetListTask();

		task.setRequest(req);
		task.addListener(new NetTaskListener<DoctorUserGetListTask.DoctorUserGetListRequest, DoctorUserGetListTask.DoctorUserGetListResponse>() {

			@Override
			public void onException(
					INetTask<DoctorUserGetListRequest, DoctorUserGetListResponse> task,
					Exception e) {
				mListView.setContextEmptyType(true, 2);
			}

			@Override
			public void onComplete(
					INetTask<DoctorUserGetListRequest, DoctorUserGetListResponse> task,
					DoctorUserGetListResponse response) {
				mListView.doComplete();
				if (response.ResultCode == 200) {
					boolean has = false;
					for (UserWrapper item : response.Res.Users) {
						boolean contain = false;
						for (UserWrapper wrapper : Users) {
							if (wrapper.Uid == item.Uid) {
								contain = true;
								break;
							}
						}
						if (!contain) {
							Users.add(item);
						}
					}

					mAdapter.setDataProvider(Users);
					mAdapter.notifyDataSetChanged();
					int size = response.Res.Users.size();
					if (size > 0) {
						mStartId = response.Res.Users.get(size - 1).Uid;
					}
				} else {
					mListView.setContextEmptyType(true, 2);
				}

			}
		});
		add(task);
	}
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/

}
